// pages/classHomePage/debate/debateMessage/debateMessage.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    people: null,
    debate_id: null,
    debate_result: null,
    group1: 'Group 1',
    group2: 'Group 2',
    group1Members: [],
    group2Members: [],
    debate_type: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    if (options != null) {
      console.log('页面跳转传值：', options)
      _this.setData({
        'debate_id': options['debateid'],
        'people': wx.getStorageSync('people'),
        'debate_type': options['type']
      })
    }
    _this.select_debate_ticket().then(() => {
      _this.student_get_groupmember()
    })

  },

  select_debate_ticket: function () {
    let _this = this
    let data = {
      'debate_id': _this.data.debate_id
    }
    return new Promise(function (resolve, reject) {
      app.formPost('/select_debate_ticket', data).then(res => {
        console.log('查询辩论会结果：', res);
        _this.setData({
          debate_result: res
        })
        resolve()
      })
    })

  },

  get_all_debate: function () {
    let data = {
      'course_name': app.globalData.banji_info['id']
    }
    app.formPost('/get_all_debate?', data).then(res => {
      console.log('获取班级所有debate:', res);
    })
  },

  supportGroup1: function () {
    let _this = this
    let data = {
      'debate_id': _this.data.debate_result['debate_id'],
      'stu_group_id': _this.data.debate_result['positive_side_student_group_id']
    }
    app.formPost('/vote_stu_group', data).then(res => {
      console.log('投票结果：', res);
      _this.onLoad()
    })
  },

  supportGroup2: function () {
    let _this = this
    let data = {
      'debate_id': _this.data.debate_result['debate_id'],
      'stu_group_id': _this.data.debate_result['negative_side_student_group_id']
    }
    app.formPost('/vote_stu_group', data).then(res => {
      console.log('投票结果：', res);
      _this.onLoad()
    })
  },

  student_get_groupmember: function () {
    let _this = this
    app.formGet('/get_all_group_member?student_group_id=' + _this.data.debate_result['positive_side_student_group_id']).then(res => {
      console.log('获取group1学生', res);
      _this.setData({
        group1Members: res
      })
    }).then(() => {
      app.formGet('/get_all_group_member?student_group_id=' + _this.data.debate_result['negative_side_student_group_id']).then(res => {
        console.log('获取group2学生', res);
        _this.setData({
          group2Members: res
        })
      })
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})